package com.lsh.oms.core.service.order.frozen;


import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.model.order.OrderShippingHeadWithBLOBs;
import com.lsh.oms.core.model.order.OrderSignHead;
import com.lsh.oms.core.proxy.service.EsProxy;
import com.lsh.oms.core.proxy.service.GoodsServiceProxy;
import com.lsh.oms.core.service.order.AbstractOrderService;
import com.lsh.oms.core.service.other.ShippingOrderService;
import com.lsh.oms.core.service.other.SignOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单发货service
 *
 * @author peter
 */
@Service
@Deprecated
public class OrderReceiptH5Service extends AbstractOrderService {

    private static Logger logger = LoggerFactory.getLogger(OrderReceiptH5Service.class);

    @Autowired
    private ShippingOrderService shippingService;

    @Autowired
    private SignOrderService signOrderService;

    @Autowired
    private GoodsServiceProxy goodsServiceProxy;

    @Autowired
    private EsProxy esProxy;


    /**
     * 创建签收单
     *
     * @param orderHead 订单信息
     * @param logSign   唯一标识
     * @return 返回信息
     */
    @Transactional
    public CommonResult<Object> createReceiptFromShip(OrderHead orderHead, String logSign) {

//        Long orderCode = orderHead.getOrderCode();
//        OrderShippingHeadWithBLOBs osh = shippingService.getBolbs(orderCode);
//        if (osh == null) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "此订单未生成发货单.请查询");
//        }
//
//        OrderSignHead oshHead = signOrderService.getSignHeadByOrderId(orderCode, false);
        CommonResult<Object> res = new CommonResult<>();
//        if (oshHead != null) {
//            if (oshHead.getPayStatus() == 2) {
//                throw new BusinessException(ReturnCode.REQUEST_FAIL, "签收单已支付不能再次签收");
//            }
//
//            res.setData(oshHead);
//            res.setCode(ReturnCode.REQUEST_SUCCESS);
//            return res;
//        }
//
//        Integer receiptTime = null;
//
//        OrderSignHead orderSignHead = this.createReceiptBillFromShip(orderHead, osh, false, receiptTime, logSign);
//        if (orderSignHead != null) {
//
//            res.setData(orderSignHead);
//
//            Integer payType = orderSignHead.getPayType();
//            boolean statusFlag = false;
//            if (payType != null && payType >= 1) {
//                statusFlag = true;
//            }
//
//            boolean kaFlag = (OrderFactory.getUserType(orderHead) == UserType.KA.getIndex());
//            this.notifyEs(orderCode, osh, OrderFactory.getMarketName(orderHead), OrderFactory.getProviderId(orderHead), logSign, statusFlag, kaFlag);
//
//            res.setCode(ReturnCode.REQUEST_SUCCESS);
////            if (kaFlag) {
////                this.smsMis(oshHead, orderHead, logSign);
////            }
//        } else {
//            res.setCode(ReturnCode.REQUEST_FAIL);
//        }

        return res;
    }

//    protected void smsMis(OrderSignHead oshHead, OrderHead orderHead, String logSign) {
//        try {
//
//            smsService.toSignSMS(oshHead, orderHead, logSign);
//
//        } catch (Exception e) {
//            logger.info("异步处理 错误");
//        }
//    }

    /**
     * @param orderHead
     * @param osh
     * @param logSign
     * @return
     */
    private OrderSignHead createReceiptBillFromShip(OrderHead orderHead, OrderShippingHeadWithBLOBs osh, boolean statusFlag, Integer receiptTime, String logSign) {
//        Long orderCode = orderHead.getOrderCode();
//        List<OrderShippingDetail> shippingDetails = shippingService.getDetails(osh.getShippingOrderId());
//
//        Map<Long, BigDecimal> orderReceiptItem = new HashMap<>();
//
//        JSONObject extjson = JSON.parseObject(orderHead.getExt());
//        String providerId = extjson.getString("providerId");
//        if (StringUtils.isBlank(providerId)) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "订单信息中没有供商信息，请联系相关人员处理");
//        }
//
//        //请求商品服务 商品信息
//        Map<Long, Good> item2GoodsMaps = this.goodsServiceProxy.requestItem2Goods(orderCode, orderHead.getParentOrderCode(), providerId, osh.getVenderId(), logSign);
//        //TODO:请求签收计算服务
//        CalOrderContent calOrderContent = this.orderSign(orderReceiptItem, osh, shippingDetails, orderHead, logSign);
//        CalOrderReceiptContent receipt = calOrderContent.getReceipt();
//        CalOrderReturnContent calOrderReturnContent = calOrderContent.getReturnContent();
//        if (calOrderReturnContent != null) {
//            logger.info(orderCode + "-returnOrder-" + JSON.toJSONString(calOrderReturnContent));
//        }
//
//        CalOrderReceiptHead head = receipt.getHead();
//        List<CalOrderReceiptDetail> detailList = receipt.getDetailList();
//        OrderSignHead oshHead = OrderFactory.createSignHead(orderCode, orderHead, osh, head, receiptTime);
//        Integer paytype = oshHead.getPayType();
//        if (paytype != null && paytype >= 1) {
//            statusFlag = true;
//        }
//        long receiptOrderId = IdGenerator.genId();
//        oshHead.setReceiptOrderId(receiptOrderId);
//
//        JSONObject extObject = head.getExt();
//        JSONObject headInfo = extObject.getJSONObject("head_info");
//        headInfo.put("floor_money", String.valueOf(oshHead.getFloorMoney().setScale(2, BigDecimal.ROUND_HALF_UP)));
//        headInfo.put("money", String.valueOf(oshHead.getMoney().setScale(2, BigDecimal.ROUND_HALF_UP)));
//        //黑狗签收  1 已签收,0未签收
//        headInfo.put("hg_receipt_status", "0");
//        extObject.put("head_info", headInfo);
//        oshHead.setExt(extObject.toJSONString());
//        List<OrderSignDetail> signDetails = OrderFactory.receiptDetails(detailList, orderHead, receiptOrderId, item2GoodsMaps, oshHead, logSign);
//
//        oshHead.setDetails(signDetails);
//
//        boolean kaFlag = (OrderFactory.getUserType(orderHead) == UserType.KA.getIndex());
//
//        if (!signOrderService.saveOrUpdateData(oshHead, logSign, statusFlag, kaFlag)) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "添加签收单失败");
//        }

        return null;
    }

    /**
     * @param orderReceiptItem
     * @param osh
     * @param shippingDetails
     * @param orderHead
     * @param logSign
     * @return
     */
//    private CalOrderContent orderSign(Map<Long, BigDecimal> orderReceiptItem, OrderShippingHeadWithBLOBs osh, List<OrderShippingDetail> shippingDetails, OrderHead orderHead, String logSign) {
//
////        CalOrderContent calOrderContent;
////        try {
////            calOrderContent = orderReceiptByItem(orderReceiptItem, osh, shippingDetails, logSign);
////            if (calOrderContent == null || calOrderContent.getReceipt() == null) {
////                logger.info(logSign + "订单号" + orderHead.getOrderCode() + ",请求签收计算服务返回报文为空。");
////                throw new BusinessException(ReturnCode.REQUEST_FAIL, "请求签收计算服务返回报文为空。");
////            }
////        } catch (Throwable e) {
////            logger.error(logSign + "订单号" + orderHead.getOrderCode() + ",请求签收计算服务失败。");
////            logger.error(e.getMessage(), e);
////            throw new BusinessException(ReturnCode.REQUEST_FAIL, "请求签收计算服务失败。");
////        }
//
//        return calOrderContent;
//    }

//    /**
//     * 通知es
//     *
//     * @param orderCode 订单号
//     * @param osh       发货单详情
//     * @param logSign   唯一标识
//     */
//    protected void notifyEs(Long orderCode, OrderShippingHeadWithBLOBs osh, String marketName, String provideId, String logSign, boolean statusFalg, boolean flagKA) {
//        OrderSignHead oshHead = signOrderService.getSignHeadByOrderId(orderCode, false);
//
//        try {
//            if (oshHead == null) {
//                logger.info("订单" + orderCode + "的签收信息为null");
//            }
//
//            if (StringUtils.isNotBlank(marketName)) {
//                oshHead.setMarketName(marketName);
//            }
//
//            oshHead.setProviderId(provideId);
//            JSONObject extObject = JSON.parseObject(oshHead.getExt());
//            JSONObject headInfo = extObject.getJSONObject("head_info");
//            logger.info("订单-" + orderCode + " 的headInfo " + headInfo);
//
//            oshHead.setFinalMoney(oshHead.getMoney());
//            logger.info(logSign + "ES入参新建签收单是-- " + JsonUtils.obj2Json(oshHead));
//            esProxy.createIndex(oshHead, logSign);
//            if (statusFalg || flagKA) {
//
//                OrderShippingHeadWithBLOBs oshEs = new OrderShippingHeadWithBLOBs();
//                oshEs.setShippingOrderId(osh.getShippingOrderId());
//                oshEs.setStatus(2);
//                logger.info(logSign + "ES入参修改发货是-- " + JsonUtils.obj2Json(oshEs));
//                esProxy.editIndex(oshEs, logSign);
//                OrderHeadForEs ohEs = new OrderHeadForEs();
//                ohEs.setOrderCode(orderCode);
//                ohEs.setOrderStatus(OrderStatus.ORDER_SIGN.getIndex());
//                logger.info(logSign + "ES入参修改订单是-- " + JsonUtils.obj2Json(ohEs));
//                esProxy.editIndex(ohEs, logSign);
//            }
//
//        } catch (Throwable e) {
//            logger.info(logSign + "订单号" + orderCode + ",生成发货单同步es失败。");
//            logger.info(e.getMessage(), e);
//        }
//    }


//    @Transactional
//    public CommonResult<Object> isCreateReceiptBill(OrderHead orderHead) {
//
//        Long orderCode = orderHead.getOrderCode();
//
//        OrderShippingHeadWithBLOBs osh = shippingService.getBolbs(orderCode);
//        if (osh == null) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "此订单未生成发货单.请查询");
//        }
//
//        CommonResult<Object> res = new CommonResult<>();
//        if (OrderFactory.getUserType(orderHead) == UserType.KA.getIndex()) {
//            return this.isCreateReceiptBillForKA();
//        } else {
//            OrderSignHead oshHead = signOrderService.getSignHeadByOrderId(orderCode, false);
//            return isCreateReceiptBillForNormal(oshHead);
//        }
//
//    }
//
//    /**
//     * @return
//     */
//    private CommonResult<Object> isCreateReceiptBillForKA() {
//        CommonResult<Object> res = new CommonResult<>();
//
//        res.setCode(ReturnCode.REQUEST_SUCCESS);
//        res.setMessage("ok");
//
//        return res;
//    }
//
//    /**
//     * @param oshHead
//     * @return
//     */
//    private CommonResult<Object> isCreateReceiptBillForNormal(OrderSignHead oshHead) {
//        CommonResult<Object> res = new CommonResult<>();
//        if (oshHead != null && oshHead.getPayStatus() == 2) {
//            res.setCode(ReturnCode.REQUEST_SUCCESS);
//            res.setMessage("ok");
//        } else {
//            res.setCode(ReturnCode.RECEIPT_NO_DEAL);
//            res.setMessage("暂无法操作签收，请先完成线上支付再重试。");
//        }
//
//        return res;
//    }

}
